package com.chen.pfm.provider;

import com.chen.pfm.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class TitleProvider extends BaseItemProvider {
    List<String> list;
    AbilitySlice abilitySlice;

    public TitleProvider(List<String> list, AbilitySlice abilitySlice) {
        this.list = list;
        this.abilitySlice = abilitySlice;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        if (list != null && i >= 0 && i < list.size()) {
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    class titleHolder {
        Text title;

        public titleHolder(Component component) {
            this.title = (Text) component.findComponentById(ResourceTable.Id_item_title);
        }
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final DirectionalLayout cpt;
        titleHolder holder = null;
        String str = list.get(i);
        if (component == null) {
            cpt = (DirectionalLayout) LayoutScatter.getInstance(abilitySlice).parse(ResourceTable.Layout_template_items_text, null, false);
            holder = new titleHolder(cpt);
            cpt.setTag(holder);
        } else {
            cpt = (DirectionalLayout) component;
            holder= (titleHolder)cpt.getTag();
        }
        holder.title.setText(str);
        return cpt;
    }
}
